package com.jair.crawler.tiger;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import okhttp3.*;
import okhttp3.OkHttpClient.Builder;

public class demo2 {
	final static long DEFAULT_TIMEOUT = 10000;
	public static void main(String[] args) throws IOException {
	    //OkHttpClient client = new OkHttpClient();
	 
	 OkHttpClient okHttpClient = getOkHttpClientBuilfer()
                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.236.103.66", 8888)))
                .build();

	    Request request = new Request.Builder()
	            .url("https://hq1.itiger.com/astock/symbol/meta/600519")
	            .header("User-Agent", "okhttp/3.6.0")
	            .header("Authorization","Bearer xw38v0jXC9b6u6UDrMfvUIBZYXjQlC")
	            .build();
	 
	    Response response = okHttpClient.newCall(request).execute();
	    if (!response.isSuccessful()) {
	        throw new IOException("服务器端错误: " + response);
	    }
	    
	    System.out.println(response.header("Server"));
	    System.out.println(response.message());
	    System.out.println(response.headers("Set-Cookie"));
	   }
	
	@SuppressWarnings("deprecation")
	public static Builder getOkHttpClientBuilfer(){
		OkHttpClient.Builder builder = new OkHttpClient.Builder();  
		builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
		SSLSocketFactory factorty = createSSLSocketFactory();
		builder.sslSocketFactory(factorty);  
		builder.hostnameVerifier(new HostnameVerifier() {

			public boolean verify(String hostname, SSLSession session) {
				// TODO Auto-generated method stub
				return true;
			}  
			 
		}); 
		return builder;
	}

private static SSLSocketFactory createSSLSocketFactory() {  
     SSLSocketFactory ssfFactory = null;  

     try {  
         SSLContext sc = SSLContext.getInstance("TLS");  
         sc.init(null, new TrustManager[]{new TrustAll()}, new SecureRandom());  

         ssfFactory = sc.getSocketFactory();  
     } catch (Exception e) {  
     }  

     return ssfFactory;  
 }  
}
